package com.hc.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.domain.UserAddr;
import com.hc.res.UserAddrItem;
import com.hc.service.UserAddrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Null;
import java.io.Serializable;
import java.util.List;

/**
 * @ClassName:UserAddrController
 * @Author:吴肖扬
 * @Date：2021/12/4 下午 02:30
 * @Describe：
 */
@Slf4j
@RestController
@RequestMapping("/userAddr")
public class UserAddrController {

    @Resource
    private UserAddrService userAddrService;
    @PostMapping
    public Result<Null> create(@RequestBody UserAddr userAddr) {
        boolean res = userAddrService.save(userAddr);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result<Null> updateById(@RequestBody UserAddr userAddr) {
        boolean res = userAddrService.updateById(userAddr);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result<Null> removeById(@PathVariable Serializable id) {
        boolean res = userAddrService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    /*
    @GetMapping("{id}")
    public Result<UserAddr> getById(@PathVariable Serializable id) {
        UserAddr res = userAddrService.getById(id);
        return ResultUtil.<UserAddr>success("查询成功").setData(res);
    }*/

    /**
     * 查寻用户的所有收获地址
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<List<UserAddrItem>> getByCondition(@PathVariable Long id) {
        List<UserAddrItem> list = userAddrService.selectUserAddrs(id);
        return ResultUtil.<List<UserAddrItem>>success("查询成功").setData(list);
    }
}
